import QtQuick 2.0
import QtQuick.Controls 2.5
import QtQuick.Layouts 1.3
import QtQml.Models 2.15
import "qrc:/CustomComponent"
import SvgImage 1.0


ListView {
    id: listViewRoot
    clip: true
    ScrollBar.vertical: ScrollBar {
        id: scrollBar
        policy: ScrollBar.AlwaysOn
    }
    model: ListModel { id: viewModel }
    property bool selected_all: true
    property int selected_count: 0
    property url source_unselect_normal: "qrc:/image/check_unselect_normal.svg"
    property url source_selected_normal: "qrc:/image/check_select_normal.svg"
    property url source_selected_disable: "qrc:/image/check_select_disable.svg"

    function initResinList( list ) {
        viewModel.clear()
        selected_count = 0
        for( var i = 0; i < list.length; i++ ){
			viewModel.append({"name": list[i], "checked": true, "enable": true})
			selected_count++
        }

        if( selected_count == viewModel.count ){
            selected_all = true
        }
    }

    function selectedAllJudge() {
	
        selected_all = true
        selected_count = 0

        for( var i = 0; i < viewModel.count; i++ ){
            if(!viewModel.get(i).checked){
                selected_all = false
            }else{
                selected_count++
            }
        }

    }

    function selectedAll( selected ){
        for( var i = 0; i < viewModel.count; i++ ){
            viewModel.get(i).checked = selected;
        }
        selected_all = selected

        if( selected_all ){ selected_count = viewModel.count }
        else{ selected_count = 0 }
    }

    function selectList() {
        var selects = []
        for( var i = 0; i < viewModel.count; i++ ){
            if(viewModel.get(i).checked){
                selects.push(viewModel.get(i).name)
            }
        }
        return selects
    }

    delegate: Rectangle {
        color: "transparent"
        implicitWidth: listViewRoot.width
        implicitHeight: 24

        RowLayout {
            anchors.fill: parent
            spacing: 6
            opacity: enabled ? 1 : 0.5

            Rectangle {
                id: selectBox
                implicitHeight: 16
                implicitWidth: 16

                SvgImage {
                    id: indicatorImage
                    anchors.fill: parent
                    enabled: model.enable
                    source: {
                        if(model.enable)
                            return model.checked ? source_selected_normal:source_unselect_normal
                        else
                           return source_selected_disable
                    }
                }

                MouseArea {
                    anchors.fill: parent
                    hoverEnabled: true
                    onClicked: {
                        if(!model.enable) return;
                        model.checked = !model.checked
                        selectedAllJudge()
                    }
                }
            }

            CusText {
                text: model.name
                color: Theme.text_color
                font.pixelSize: Theme.font_size_13

                font.styleName: Theme.font_family_regular
            }

            Item {
                Layout.fillWidth: true
            }
        }
    }
}
